package com.wcbdota.list;

import java.util.HashMap;
import java.util.List;

import com.wcbdota.R;
import com.wcbdota.base.C;
import com.wcbdota.model.Items;
import com.wcbdota.util.AppCache;
import com.wcbdota.util.AppUtil;
import com.wcbdota.util.AsyncImageLoader;
import com.wcbdota.util.DataCheck;
import com.wcbdota.util.SDUtil;
import com.wcbdota.util.AsyncImageLoader.ImageCallback;


import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemGridAdapterList extends BaseAdapter {
	
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	private List<Items> ItemData;
	private GridView gridView;
	private AsyncImageLoader asyncImageLoader;
	
	
	private final class  ViewHolder{
		private ImageView mItemImg;
		private TextView mItemName;
		private TextView mIdesc;
		private TextView mPrice;
	}
	
	public ItemGridAdapterList(Context context,List<Items> ItemData,GridView gridView){
		this.mLayoutInflater = LayoutInflater.from(context);
		this.mContext = context;
		this.ItemData = ItemData;
		this.gridView = gridView;
		asyncImageLoader = new AsyncImageLoader();
	}

	@Override
	public int getCount() {
		return ItemData.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//获取GridView里的组件（用ViewHolder）
		ViewHolder mHolder;
		Bitmap cachedImage;
		if(convertView==null){
			convertView=mLayoutInflater.inflate(R.layout.item_gridview_item, parent, false);
			mHolder = new ViewHolder();
			mHolder.mItemImg=(ImageView) convertView.findViewById(R.id.iv_item_img);
			mHolder.mItemName=(TextView) convertView.findViewById(R.id.tv_item_name);
			mHolder.mIdesc=(TextView)convertView.findViewById(R.id.tv_desc);
			mHolder.mPrice=(TextView)convertView.findViewById(R.id.tv_price);
			convertView.setTag(mHolder);
		}else{
			mHolder=(ViewHolder) convertView.getTag();
		}
		
		String imageUrl = C.remoteres.base + C.remoteres.items + "/" + ItemData.get(position).getImg() + ".png";
		mHolder.mItemImg.setTag(imageUrl);
		//赋值图片用bitmap 
		String cacheKey = AppUtil.md5(imageUrl);
		cachedImage = SDUtil.getImage(cacheKey);
		if (cachedImage == null) {
			Log.w("gird", "get cached image null");
			cachedImage = asyncImageLoader.loadBitmap(imageUrl, new ImageCallback() {

				@Override
				public void imageLoadSuccess(Bitmap imageBitmap,
						String imageUrl, View view) {
					ImageView imageViewByTag = (ImageView) gridView.findViewWithTag(imageUrl);  
					if (imageViewByTag != null) {
						String cacheKey = AppUtil.md5(imageUrl);
						SDUtil.saveImage(imageBitmap, cacheKey);
						imageViewByTag.setImageBitmap(imageBitmap);
					}
				}

				@Override
				public void imageLoadError(String imageUrl, View view) {
					// TODO Auto-generated method stub
					
				}
			});
		}

		if (cachedImage == null) {  
			mHolder.mItemImg.setImageResource(R.drawable.img_loading);  
			Log.e("Adapter", "null");
		}
		else {
			mHolder.mItemImg.setImageBitmap(cachedImage);
		}
		
		mHolder.mItemName.setText((String) ItemData.get(position).getName().toString());
		mHolder.mIdesc.setText(DataCheck.getData(ItemData.get(position).getDesc().toString()));
		mHolder.mPrice.setText(ItemData.get(position).getPrice().toString());
		return convertView;
	}

}
